import { getAccountSetId, getToken } from '@/utils/storage';

// 文件下载
export function downloadFile(id: number | string, filename: string) {
  const downloadUrl = `/api/system/oss/download/${id}`;
  const x = new XMLHttpRequest();
  x.open('GET', downloadUrl, true);
  x.responseType = 'blob';
  x.setRequestHeader('Authorization', `${getToken()}`);
  x.onload = function () {
    const url = window.URL.createObjectURL(x.response);
    const a = document.createElement('a');
    a.href = url;
    a.download = filename;
    a.click();
  };
  x.send();
}

// 不同的业务api模块导出业务列表
export function getBusinessList(downloadUrl: any, filename: string) {
  const x = new XMLHttpRequest();
  x.open('GET', downloadUrl, true);
  x.responseType = 'blob';
  x.setRequestHeader('Authorization', `${getToken()}`);
  x.setRequestHeader('account-set-id', `${getAccountSetId()}`);
  x.onload = function () {
    const url = window.URL.createObjectURL(x.response);
    const a = document.createElement('a');
    a.href = url;
    a.download = filename;
    a.click();
  };
  x.send();
}

// 不同的业务api模块导出业务列表
export function exportList(downloadUrl: any, data: any, filename?: string) {
  const x = new XMLHttpRequest();
  x.open('POST', downloadUrl, true);
  x.responseType = 'blob';
  x.setRequestHeader('Authorization', `${getToken()}`);
  x.setRequestHeader('account-set-id', `${getAccountSetId()}`);
  x.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
  x.onload = function () {
    const url = window.URL.createObjectURL(x.response);
    const a = document.createElement('a');
    a.href = url;
    a.download = filename ?? '导出文件';
    a.click();
  };
  x.send(data);
}
